package com.cml.domain.service;

import com.alibaba.fastjson.JSONObject;
import com.cml.common.constant.MessageType;
import com.cml.common.constant.NodeStatus;
import com.cml.common.context.ServerContext;
import com.cml.common.util.SoltMathUtil;
import com.cml.domain.entity.DefaultMessage;
import com.cml.domain.entity.ServerNode;
import com.cml.domain.entity.Solt;
import com.cml.common.dto.entity.SoltMetadata;
import com.cml.infrastructure.MessageQueueManager;
import com.cml.infrastructure.NetworkManager;
import org.apache.log4j.Logger;

import java.util.*;

/**
 * 姓名：陈茂林
 * 时间：2023/10/11 13:56
 * 描述：
 */
public class SoltContextManagerService {
    private ServerNode curServerNode;

    private NetworkManager networkManager;

    private SoltManagerService soltManagerService;

    private SoltReplicasManagerService soltReplicasManagerService;


    Logger logger = Logger.getLogger(SoltContextManagerService.class);
    private List<SoltMetadata> slotsAllocationReplicas =
            new ArrayList();

    public SoltContextManagerService(ServerContext serverContext){
        this.curServerNode = serverContext.getServerNode();
        this.networkManager=serverContext.getNetworkManager();
        this.soltManagerService = new SoltManagerService(serverContext,this);
        this.soltReplicasManagerService = new SoltReplicasManagerService(serverContext,this);
    }

    public void startSoltManagerProcess(){

        try{
            logger.info("------------------开始进行solt分配管理-------------------");
            soltManagerService.startSoltManagerProcess();
            logger.info("------------------结束solt分配管理-------------------");
            logger.info("------------------开始进行solt副本分配管理-------------------");
            soltReplicasManagerService.startSoltManagerProcess();
            logger.info("------------------结束solt副本分配管理-------------------");
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            NodeStatus.getInstance().setCurStatus(NodeStatus.NODE_FAIL);
        }
    }


    public Solt getServiceNameSolt(String serviceName){
        Solt solt = soltManagerService.getServiceNameSolt(serviceName);
        //如果solt，就从副本里面获取
        if(solt == null){
            solt = soltReplicasManagerService.getServiceNameSolt(serviceName);
        }else{
            List<SoltMetadata> curServernodeReplicasRelationship=soltReplicasManagerService.getCurServernodeReplicasRelationship();
            for (SoltMetadata soltMetadata:curServernodeReplicasRelationship) {
                if(solt.getSolt()>=soltMetadata.getStartSlot() && solt.getSolt()<=soltMetadata.getEndSlot()){
                    solt.setReplicasServerNodeId(soltMetadata.getServerNodeId());
                }
            }

        }

        return solt;
    }

    public List<SoltMetadata> getSlotsAllocation(){
        return soltManagerService.getSlotsAllocation();
    }

    public List<SoltMetadata> getSlotsReplicasAllocation(){
        return  soltReplicasManagerService.getSlotsAllocation();
    }

























}
